//
//  RegisterView.m
//  OCProject
//
//  Created by mqwang on 2018/3/6.
//  Copyright © 2018年 oc. All rights reserved.
//

#import "RegisterView.h"

@interface RegisterView()<UITextFieldDelegate>

//@property(nonatomic, strong) UITextField *tfUser;
@property(nonatomic, strong) UITextField *tfCode;
@property(nonatomic, strong) UITextField *tfPwd;
@property(nonatomic, strong) UITextField *tfAgainPwd;
@property(nonatomic, strong) UIButton *btnCode;

@end

@implementation RegisterView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addUI];
    
    }
    return self;
    
}

-(void)addUI{
    
   UIView *view = [[UIView alloc] init];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(25);
        make.right.equalTo(self.mas_right).offset(-25);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        
    }];
    
    UILabel *phoneLab = [[UILabel alloc] init];
    phoneLab.layer.borderColor = [NAV_BAR_COLOR CGColor];
    phoneLab.layer.borderWidth = 0.5f;
    phoneLab.layer.cornerRadius = BUTTON_CORNER_RADIUS;
    [self addSubview:phoneLab];
    [phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(30);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
        make.height.equalTo(@45);
        
    }];
    
    UIImageView *phoneIv = [[UIImageView alloc] init];
    phoneIv.image = [UIImage imageNamed:@"denglu_mima"];
    [self addSubview:phoneIv];
    [phoneIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLab.mas_left).offset(15);
        make.width.equalTo(@11);
        make.height.equalTo(@14);
        make.centerY.equalTo(phoneLab);
        
    }];
    
    self.btnCode = [UIButton buttonWithType:UIButtonTypeSystem];
    self.btnCode.titleLabel.font = FONT_28PX;
    self.btnCode.titleLabel.textColor = [UIColor yellowColor];
    [self.btnCode setTitleColor:NAV_BAR_COLOR forState:UIControlStateNormal];
    [self.btnCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    //    self.btnCode.backgroundColor = [UIColor tr]
    [self addSubview:self.btnCode];
    [self.btnCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(phoneLab.mas_right).offset(-15);
        make.top.equalTo(phoneLab.mas_top);
        make.bottom.equalTo(phoneLab.mas_bottom);
        
    }];
    
    self.tfUser = [[UITextField alloc ] init];
    self.tfUser.placeholder = @"请输入手机号";
    self.tfUser.font = FONT_28PX;
    self.tfUser.delegate = self;
    [self addSubview:self.tfUser];
    [self.tfUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneIv.mas_right).offset(10);
//        make.right.equalTo(self.btnCode.mas_left);
        make.top.equalTo(phoneLab.mas_top);
        make.bottom.equalTo(phoneLab.mas_bottom);
        make.width.equalTo(@150);
//        make.centerX.equalTo(phoneLab);
        
    }];
    
    UILabel *labCode = [[UILabel alloc] init];
    labCode.layer.borderColor = [NAV_BAR_COLOR CGColor];
    labCode.layer.borderWidth = 0.5f;
    labCode.layer.cornerRadius = BUTTON_CORNER_RADIUS;
    [self addSubview:labCode];
    [labCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneLab.mas_bottom).offset(15);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
        make.height.equalTo(@45);
    }];
    
    UIImageView *ivCode = [[UIImageView alloc] init];
    ivCode.image = [UIImage imageNamed:@"zhuce_yanzhengma"];
    [self addSubview:ivCode];
    [ivCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@11);
        make.height.equalTo(@14);
        make.left.equalTo(labCode.mas_left).offset(15);
        make.centerY.equalTo(labCode);

    }];
    
    self.tfCode = [[UITextField alloc] init];
    self.tfCode.placeholder = @"请输入验证码";
    self.tfCode.delegate = self;
    self.tfCode.font = FONT_28PX;
    [self addSubview:self.tfCode];
    [self.tfCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ivCode.mas_right).offset(15);
        make.right.equalTo(labCode.mas_right);
        make.centerY.equalTo(labCode);
        
    }];
    
    UILabel *labPwd = [[UILabel alloc] init];
    labPwd.layer.borderWidth = 0.5f;
    labPwd.layer.borderColor = [NAV_BAR_COLOR CGColor];
    labPwd.layer.cornerRadius = BUTTON_CORNER_RADIUS;
    [self addSubview:labPwd];
    [labPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labCode.mas_bottom).offset(15);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
        make.height.equalTo(@45);

    }];
    
    UIImageView *ivPwd = [[UIImageView alloc] init];
    ivPwd.image = [UIImage imageNamed:@"denglu_mima"];
    [self addSubview:ivPwd];
    [ivPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labPwd.mas_left).offset(15);
        make.centerY.equalTo(labPwd);

    }];
    
    self.tfPwd = [[UITextField alloc] init];
    self.tfPwd.placeholder = @"请输入密码";
    self.tfPwd.font = FONT_28PX;
    [self addSubview:self.tfPwd];
    [self.tfPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ivPwd.mas_right).offset(15);
        make.right.equalTo(labPwd.mas_right);
        make.centerY.equalTo(labPwd);

    }];
    
    UILabel *labConfirmPwd = [[UILabel alloc] init];
    labConfirmPwd.layer.borderWidth = 0.5f;
    labConfirmPwd.layer.borderColor = [NAV_BAR_COLOR CGColor];
    labConfirmPwd.layer.cornerRadius = BUTTON_CORNER_RADIUS;
    [self addSubview:labConfirmPwd];
    [labConfirmPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labPwd.mas_bottom).offset(15);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
        make.height.equalTo(@45);
        
    }];
    
    UIImageView *ivConfirmPwd = [[UIImageView alloc] init];
    ivConfirmPwd.image = [UIImage imageNamed:@"denglu_mima"];
    [self addSubview:ivConfirmPwd];
    [ivConfirmPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labConfirmPwd.mas_left).offset(15);
        make.centerY.equalTo(labConfirmPwd);
        
    }];
    
    self.tfAgainPwd = [[UITextField alloc] init];
    self.tfAgainPwd.placeholder = @"请确认密码";
    self.tfAgainPwd.font = FONT_28PX;
    [self addSubview:self.tfAgainPwd];
    [self.tfAgainPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ivConfirmPwd.mas_right).offset(15);
        make.right.equalTo(labConfirmPwd.mas_right);
        make.centerY.equalTo(labConfirmPwd);
        
    }];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"立即注册" forState:UIControlStateNormal];
    [btn setTitleColor:WHITE_TITLE_COLOR forState:UIControlStateNormal];
    btn.titleLabel.font = FONT_36PX;
    btn.backgroundColor = NAV_BAR_COLOR;
    btn.layer.cornerRadius = BUTTON_CORNER_RADIUS;
//    [btn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(registerAciton) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labConfirmPwd.mas_bottom).offset(30);
        make.left.equalTo(view);
        make.right.equalTo(view);
        make.height.offset(44);
        
    }];
    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.tfUser){
        NSUInteger lengthOfString = string.length;
        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入
            unichar character = [string characterAtIndex:loopIndex];
            if (character < 48) return NO; // 48 unichar for 0
            if (character > 57) return NO; // 57 unichar for 9
        }
        // Check for total length
        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
        if (proposedNewLength > 11) return NO;//限制长度
        return YES;
    }else if(textField == self.tfCode){
        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
        NSUInteger lengthOfString = string.length;
        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入
            unichar character = [string characterAtIndex:loopIndex];
            if (character < 48) return NO; // 48 unichar for 0
            if (character > 57) return NO; // 57 unichar for 9
        }
        if ( proposedNewLength > 20) return NO;//限制长度
        return YES;
    }else {
        return YES;
    }
}

-(void)registerAciton{
    if(self.btnCallbackHandle){
        self.btnCallbackHandle();
    }
    
}

+(void)abc{
    [SVProgressHUD showErrorWithStatus:@"error"];
}

@end
